package com.tledu.springbootmybatis.controller;

import com.tledu.springbootmybatis.core.AjaxResult;
import com.tledu.springbootmybatis.core.constants.CommonConstants;
import com.tledu.springbootmybatis.domain.User;
import com.tledu.springbootmybatis.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author cyrus
 */
@Controller
@AllArgsConstructor
public class LoginController {

    private final IUserService userService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public AjaxResult<User> login(String username, String password, HttpSession session) {
        User user = userService.login(username, password);
        session.setAttribute(CommonConstants.LOGIN_USER_SESSION_KEY, user);
        return new AjaxResult<>(user);
    }

}
